// pages/address-info/address-info.js
const common = require('../../utils/common.js');
const api = require('../../api/api.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    ordercateList: [{
      id: '0',
      name: '快速配送',
      selected: false
    }, {
      id: '1',
      name: '门店自取',
      selected: true
    }],
    addrList: [],
    winWidth: 0,
    winHeight: 0,
    currentTab: 0,
    selected: '',
    shopInfo: '',
    mydata: '',
  },

  bindChange: function (e) {

    var that = this;
    that.setData({
      currentTab: e.detail.current
    });

  },
  swichNav: function (e) {

    var that = this;

    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.target.dataset.current
      })
    }
  },
  //获取地址列表
  get_addrList() {
    let that = this;
    common.request(api.get_addrList, {}, 'post').then((res) => {
      if (res.status == 1) {
        console.log(res)
        that.setData({
          addrList: res.addrlist,
        })
      }
    })
  },
  address_add() {
    wx.navigateTo({
      url: '/pages/address-add/address-add',
    })
  },
  address_editpeisong(e) {
    console.log(e)
    wx.navigateTo({
      url: '/pages/address-add/address-add?addr_id=' + e.currentTarget.dataset.id,
    })
  },
  address_edit() {
    wx.navigateTo({
      url: '/pages/address-edit/address-edit',
    })
  },
  address_add_ziti() {
    wx.navigateTo({
      url: '/pages/addziti/addziti',
    })
  },
  address_edit_ziti() {
    wx.navigateTo({
      url: '/pages/addziti/addziti?edit=1',
    })
  },
  //删除地址
  Del_address(e) {
    let that = this;
    wx.showModal({
      title: '',
      content: '确定要删除地址？',
      success: function (res) {
        if (res.confirm) {
          let addr_id = e.currentTarget.dataset.id;
          common.request(api.Delet_address, {
            addr_id: addr_id
          }, 'POST').then(function (res) {
            if (res.status == 1) {
              common.showSuccessToast('删除成功');
              setTimeout(function () {
                that.get_addrList();
              }, 1500)
            } else {
              common.showSuccessToast(res.msg);
            }
          });
        }
      }
    })
    return false;
  },

  get_userShop(e) {
    let that = this;
    common.request(api.get_userShop, {}, 'post').then((res) => {
      if (res.status == 1) {
        that.setData({
          shopList: res.shoplist
        })
      }
    })
  },

  //获取我的自提门店
  get_myzitiShop() {
    let that = this;
    let uid = wx.getStorageSync('user_id');
    common.request(api.get_myzitiShop, {
      uid: uid
    }, 'post').then((res) => {
      if (res.status == 1) {
        that.setData({
          shopInfo: res.zitiinfo,
        })
      }
    })
  },
  choice_address_ziti(e) {
    let that = this;
    console.log(e)
    if (that.data.type == 'member') {

    } else {
    var pages = getCurrentPages(); // 获取页面栈
    var currPage = pages[pages.length - 1]; // 当前页面
    var prevPage = pages[pages.length - 2]; // 上一个页面
    prevPage.setData({
      mydata_ziti: {
        shop_id: e.currentTarget.dataset.shop_id,
        user_name: e.currentTarget.dataset.user_name,
        user_tel: e.currentTarget.dataset.user_tel,
        shop_name: e.currentTarget.dataset.shop_name,
        shop_addr: e.currentTarget.dataset.shop_addr,
        currenttab: e.currentTarget.dataset.currenttab
      },
      mydata:''
    })
    wx.navigateBack({
      delta: 1
    })
  }
  },
  choice_address(e) {
    console.log(e)
    let that = this
    if (that.data.type == 'member') {

    } else {
      console.log(e)
      var pages = getCurrentPages(); // 获取页面栈
      var currPage = pages[pages.length - 1]; // 当前页面
      var prevPage = pages[pages.length - 2]; // 上一个页面
      prevPage.setData({
        mydata: {
          id: e.currentTarget.dataset.id,
          shouhuo_area: e.currentTarget.dataset.shouhuo_area,
          shouhuo_detailarea: e.currentTarget.dataset.shouhuo_detailarea,
          shouhuo_name: e.currentTarget.dataset.shouhuo_name,
          shouhuo_tel: e.currentTarget.dataset.shouhuo_tel,
          currenttab: e.currentTarget.dataset.currenttab,
          is_default: e.currentTarget.dataset.is_default,

        },
        mydata_ziti:''
      })
      wx.navigateBack({
        delta: 1
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.type) {
      this.setData({
        type: options.type,
      })
    }
    if (options.currentTab) {
      this.setData({
        currentTab: options.currentTab
      })
    }
  },



  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

    this.get_addrList();
    this.get_myzitiShop();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})